<template>
  <div class="login-container">
    <!-- 语言切换器 -->
    <div class="language-switcher-wrapper">
      <LanguageSwitcher />
    </div>
    
    <div class="login-content">
      <div class="login-left">
        <div class="brand">
          <ClockCircleOutlined class="brand-icon" />
          <h1>{{ $t('login.brand.title') }}</h1>
          <p class="slogan">{{ $t('login.brand.slogan') }}</p>
        </div>
        <div class="features">
          <div class="feature-item">
            <CheckCircleOutlined />
            <span>{{ $t('login.brand.features.easy') }}</span>
          </div>
          <div class="feature-item">
            <LineChartOutlined />
            <span>{{ $t('login.brand.features.monitor') }}</span>
          </div>
          <div class="feature-item">
            <SafetyCertificateOutlined />
            <span>{{ $t('login.brand.features.secure') }}</span>
          </div>
        </div>
      </div>
      
      <div class="login-right">
        <div class="login-box">
          <h2>{{ $t('login.title') }}</h2>
          <p class="subtitle">{{ $t('login.subtitle') }}</p>
          
          <a-form
            ref="formRef"
            :model="loginForm"
            :rules="rules"
            layout="vertical"
            @finish="handleSubmit"
            class="login-form"
          >
            <a-form-item name="username">
              <a-input
                v-model:value="loginForm.username"
                size="large"
                :placeholder="$t('login.username')"
              >
                <template #prefix>
                  <UserOutlined class="input-icon" />
                </template>
              </a-input>
            </a-form-item>
            
            <a-form-item name="password">
              <a-input-password
                v-model:value="loginForm.password"
                size="large"
                :placeholder="$t('login.password')"
              >
                <template #prefix>
                  <LockOutlined class="input-icon" />
                </template>
              </a-input-password>
            </a-form-item>
            
            <div class="form-footer">
              <a-checkbox v-model:checked="loginForm.remember">
                {{ $t('login.remember') }}
              </a-checkbox>
            </div>
            
            <a-button
              type="primary"
              html-type="submit"
              size="large"
              :loading="loading"
              class="login-button"
            >
              {{ $t('login.loginButton') }}
            </a-button>
          </a-form>
          
          <p class="help-text">
            <InfoCircleOutlined />
            {{ $t('login.helpText') }}
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { message } from 'ant-design-vue'
import { useAuthStore, type LoginForm } from '@/stores/auth'
import LanguageSwitcher from '@/components/LanguageSwitcher.vue'
import {
  ClockCircleOutlined,
  UserOutlined,
  LockOutlined,
  CheckCircleOutlined,
  LineChartOutlined,
  SafetyCertificateOutlined,
  InfoCircleOutlined
} from '@ant-design/icons-vue'

const { t } = useI18n()
const router = useRouter()
const authStore = useAuthStore()

const formRef = ref()
const loading = ref(false)

const loginForm = reactive<LoginForm>({
  username: '',
  password: '',
  remember: true
})

const rules = computed(() => ({
  username: [
    { required: true, message: t('login.usernameRequired'), trigger: 'blur' }
  ],
  password: [
    { required: true, message: t('login.passwordRequired'), trigger: 'blur' }
  ]
}))

const handleSubmit = async () => {
  try {
    await formRef.value?.validate()
    loading.value = true
    
    const success = await authStore.login(
      loginForm.username,
      loginForm.password
    )

    if (success) {
      router.push('/')
    } else {
      message.error(t('login.loginFailed'))
    }
  } catch (error: any) {
    if (error.message) {
      message.error(error.message)
    }
  } finally {
    loading.value = false
  }
}
</script>

<style lang="less" scoped>
.login-container {
  min-height: 100vh;
  background: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  position: relative;
}

.language-switcher-wrapper {
  position: absolute;
  top: 24px;
  right: 24px;
  z-index: 10;
}

.login-content {
  display: flex;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
  width: 1000px;
  max-width: 90%;
  min-height: 600px;
  overflow: hidden;
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #1890ff 0%, #0050b3 100%);
  color: white;
  padding: 60px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.brand {
  text-align: left;
}

.brand-icon {
  font-size: 48px;
  margin-bottom: 24px;
}

.brand h1 {
  font-size: 36px;
  font-weight: 600;
  margin: 0 0 16px 0;
}

.slogan {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 16px;
  font-size: 16px;
  
  .anticon {
    font-size: 20px;
    color: #69c0ff;
  }
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
}

.login-box {
  width: 100%;
  max-width: 400px;
}

.login-box h2 {
  font-size: 28px;
  font-weight: 600;
  color: #262626;
  margin: 0 0 8px 0;
  text-align: center;
}

.subtitle {
  color: #8c8c8c;
  text-align: center;
  margin: 0 0 40px 0;
  font-size: 16px;
}

.login-form {
  .input-icon {
    color: #bfbfbf;
  }
}

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 24px 0;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
}

.help-text {
  text-align: center;
  color: #8c8c8c;
  margin: 24px 0 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  
  .anticon {
    color: #1890ff;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-content {
    flex-direction: column;
    width: 100%;
    min-height: auto;
  }
  
  .login-left {
    padding: 40px 30px;
  }
  
  .login-right {
    padding: 40px 30px;
  }
  
  .features {
    display: none;
  }
  
  .language-switcher-wrapper {
    top: 16px;
    right: 16px;
  }
}
</style> 